package com.zysc.processflow.controller;


import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zysc.processflow.common.base.ResultBody;
import com.zysc.processflow.entity.Handleprocess;
import com.zysc.processflow.pojo.HandleprocessPo;
import com.zysc.processflow.pojo.ProcessConfig;
import com.zysc.processflow.service.IHandleprocessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import com.zysc.processflow.common.base.BaseController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 处置流程管理 前端控制器
 * </p>
 *
 * @author 正元智慧城市_
 * @since 2022-11-18
 */
@Api(value = "处置流程管理", tags = "处置流程管理")
@RestController
@RequestMapping("/handleprocess")
public class HandleprocessController extends BaseController<Handleprocess> {

    @Autowired
    IHandleprocessService service;

    public HandleprocessController(IHandleprocessService service) {
        super(service);
    }

    @ApiOperation("获取处置流程管理列表(支持分页)")
    @GetMapping (value = "GetHandleprocessList")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "pageIndex", name = "pageIndex", dataType = "String"),
            @ApiImplicitParam(value = "pageSize", name = "pageSize", dataType = "String"),
            @ApiImplicitParam(value = "流程名称", name = "name", dataType = "String"),
            @ApiImplicitParam(value = "流程编号", name = "processno", dataType = "String"),
    })
    public ResultBody GetHandleprocessList(Integer pageIndex , Integer pageSize , String name , String processno,String id) {
        if (pageIndex != null && pageSize != null) {
            IPage<Handleprocess> page = new Page<>(pageIndex, pageSize);
            List<HandleprocessPo> res = service.GetHandleprocessListByPage(page, name,processno,id);
            return ResultBody.Success(res, page.getTotal());
        } else {
            List<HandleprocessPo> res = service.GetHandleprocessListByPage(null, name,processno,id);
            return ResultBody.Success(res);
        }
    }

    @ApiOperation("获取前端配置文件")
    @GetMapping (value = "getConfigByProcessId")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "processId", name = "processId", dataType = "String"),
    })
    public ResultBody getConfigByProcessId(String processId) {
        return service.getConfigByProcessId(processId);
    }

    @ApiOperation("添加/更新处置流程管理实体,包含审核节点配置,抄送人配置")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "处置流程管理实体", name = "entity", dataType = "Handleprocess")
    })
    @Transactional
    @PostMapping(value = "SaveOrUpdateProcess")
    public ResultBody SaveOrUpdateProcess(@RequestBody ProcessConfig entity) {
        try{
            //0.涉及四个表:handleprocess,handleprocessnode,handleprocessline
            //1.判断当前有没有该流程正在被使用
            Boolean b1 = service.hasRunningRecord(entity.getId());
            //2.修改或者新增handleprocess
            Handleprocess handleprocess = new Handleprocess();
            handleprocess.setId(entity.getId());
            handleprocess.setType(entity.getType());
            handleprocess.setName(entity.getName());
            handleprocess.setDescription(entity.getDescription());
            handleprocess.setProcessno(entity.getProcessno());
            handleprocess.setConfig(JSON.toJSONString(entity));
            Boolean b2 = service.saveOrUpdate(handleprocess);
            //3.处理handleprocessnode表中,该流程的所有审核流程
            Boolean b3 = service.handleNodes(entity);
            //4.处理handleprocessline
            Boolean b4 = service.handleLines(entity);
            if (!b1 || !b2 || !b3 || !b4) {
                throw new RuntimeException("修改数据失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultBody.error(e.getMessage(), 500);
        }
        return ResultBody.Success(entity);
    }
    @ApiOperation("添加/更新处置流程管理实体")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "处置流程管理实体", name = "entity", dataType = "Handleprocess")
    })
    @Transactional
    @PostMapping(value = "SaveOrUpdate")
    public ResultBody SaveOrUpdate(@RequestBody Handleprocess entity) {
        Boolean b = service.saveOrUpdate(entity);
        if(b){
            return ResultBody.Success(entity);
        }else {
            return ResultBody.error("添加/更新数据失败", 500);
        }
    }

    @ApiOperation("删除处置流程管理记录")
    @PostMapping("DeleteHandleprocess")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "处置流程管理实体", name = "entity", dataType = "Handleprocess")
    })
    @Transactional
    public ResultBody DeleteHandleprocess(@RequestBody String processId) {
        try{
            //0.涉及三个表:handleprocess,handleprocessnode, handleprocesscc
            //1.判断当前有没有该流程正在被使用
            Boolean b1 = service.hasRunningRecord(processId);
            //2.删除handleprocess表
            boolean b2 = service.removeById(processId);
            //3.删除handleprocessnode表
            Boolean b3 = service.deleteProcessNodes(processId);
            //4.删除handleprocesscc表
            Boolean b4 = service.deleteProcessCcs(processId);
            //5.删除handleprocessline表
            Boolean b5 = service.deleteProcessLines(processId);
            if (!b1 || !b2 || !b3|| !b4 ||!b5) {
                throw new RuntimeException("修改数据失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultBody.error(e.getMessage(), 500);
        }
        return ResultBody.Success(true);
    }
}
